package com.sh.rbac.controller;

import com.sh.rbac.core.common.annotation.SysLog;
import com.sh.rbac.core.common.enums.OperationTypeEnum;
import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.domain.dto.file.FileQueryDTO;
import com.sh.rbac.domain.entity.File;
import com.sh.rbac.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件控制器
 *
 * @author wb
 * @since 2024-10-01
 */
@RestController
@RequestMapping("/sys/file")
public class FileController {

    @Autowired
    private FileService fileService;


    @Operation(summary = "分页查询文件列表", description = "分页条件查询文件列表信息")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = File.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("@ps.hasPerm('sys:file:page')")
    public Result<?> getPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody FileQueryDTO fileQueryDTO) {
        return Result.success(fileService.getPage(current, limit, fileQueryDTO));
    }


    @SysLog(module = "系统管理->文件管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除文件", description = "根据ID删除文件信息")
    @ApiResponse(responseCode = "200", description = "成功删除文件")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("@ps.hasPerm('sys:file:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return fileService.removeById(id) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->文件管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除文件", description = "批量删除文件信息")
    @ApiResponse(responseCode = "200", description = "成功删除文件")
    @DeleteMapping("batch-delete")
    @PreAuthorize("@ps.hasPerm('sys:file:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return fileService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }


    @Operation(summary = "上传文件", description = "上传文件信息")
    @ApiResponse(responseCode = "200", description = "成功上传文件")
    @SysLog(module = "系统管理->文件管理")
    @PostMapping("upload")
    public Result<String> upload(@RequestPart("file") MultipartFile file) {
        return Result.success(fileService.upload(file));
    }

    @Operation(summary = "下载文件", description = "下载文件信息")
    @ApiResponse(responseCode = "200", description = "成功下载文件")
    @PreAuthorize("@ps.hasPerm('sys:file:download')")
    @SysLog(module = "系统管理->文件管理")
    @GetMapping("download")
    public void download(@RequestParam("filename") String filename, HttpServletResponse response) {
        fileService.download(filename, response);
    }
}
